﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using VCDIFF;
using VCDIFF.Commands;
using VCDIFF.Rebuild;
using System.Collections.Generic;
using System;
using System.IO;

namespace VCDIFF.Tests
{
    
    
    /// <summary>
    ///This is a test class for RebuildInstructionsTest and is intended
    ///to contain all RebuildInstructionsTest Unit Tests
    ///</summary>
    [TestClass()]
    public class RebuildInstructionsTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        /// <summary>
        ///A test for RebuildInstructions Constructor
        ///</summary>
        [TestMethod()]
        public void RebuildInstructionsConstructorTest()
        {
            RebuildInstructions target = new RebuildInstructions();
        }

        /// <summary>
        ///A test for Rebuild
        ///</summary>
        [TestMethod()]
        public void RebuildTest()
        {
            byte[] data = new byte[] {
                0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 
                0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75
            };

            string sourceFileName = "source_RebuildTest.txt";
            File.WriteAllBytes(sourceFileName, data);

            Add add = new Add(4, new byte[4] { 0x50, 0x51, 0x52, 0x53 });
            Run run = new Run(2, 0x54);
            SourceFile source = new SourceFile(data);
            Copy copy = new Copy(8, 16, source);
            Noop noop = new Noop();                        

            IList<Command> expected = new List<Command>();
            expected.Add(add);
            expected.Add(run);
            expected.Add(copy);
            expected.Add(noop);
            expected.Add(noop);

            RebuildInstructions target = new RebuildInstructions();
            IList<Command> actual = target.Rebuild(InitializeWindow(), sourceFileName);
            bool same = true;

            if (expected.Count != actual.Count)
                same = false;
            else
            {
                if (!expected[0].GetType().Equals(actual[0].GetType()))
                    same = false;
                else
                {
                    if (((Add)expected[0]).Sequence.Length != ((Add)actual[0]).Sequence.Length)
                        same = false;
                    else
                        for (int i = 0; i < ((Add)expected[0]).Sequence.Length; i++)
                            if (((Add)expected[0]).Sequence[i] != ((Add)actual[0]).Sequence[i])
                                same = false;
                    if (((Add)expected[0]).Size != ((Add)expected[0]).Size)
                        same = false;
                }

                if (!expected[1].GetType().Equals(actual[1].GetType()))
                    same = false;
                else
                {
                    if (((Run)expected[1]).RunByte != ((Run)actual[1]).RunByte)
                        same = false;
                    if (((Run)expected[1]).Size != ((Run)actual[1]).Size)
                        same = false;
                }

                if (!expected[2].GetType().Equals(actual[2].GetType()))
                    same = false;
                else
                {
                    if (((Copy)expected[2]).Size != ((Copy)actual[2]).Size)
                        same = false;
                    if (((Copy)expected[2]).Address != ((Copy)actual[2]).Address)
                        same = false;
                    if (((Copy)expected[2]).Source.Source.Length != ((Copy)actual[2]).Source.Source.Length)
                        same = false;
                    else
                        for (int i = 0; i < ((Copy)expected[2]).Source.Source.Length; i++)
                            if (((Copy)expected[2]).Source.Source[i] != ((Copy)actual[2]).Source.Source[i])
                                same = false;
                }

                if (!expected[3].GetType().Equals(actual[3].GetType()))
                    same = false;

                if (!expected[4].GetType().Equals(actual[4].GetType()))
                    same = false;
            }

            Assert.AreEqual(same, true);
        }

        /// <summary>
        /// This method initializes a new window
        /// </summary>
        /// <returns>A initialized window</returns>
        public Window InitializeWindow()
        {
            byte[] windowData = new byte[32];
            windowData[0] = 0x01;       // Window indicator
            windowData[1] = 0x8B;       // Source segment size
            windowData[2] = 0x2E;
            windowData[3] = 0x00;       // Source segment position
            windowData[4] = 0x1B;       // Delta encoding length
            windowData[5] = 0x96;       // Size of the target window
            windowData[6] = 0x5D;
            windowData[7] = 0x00;       // Delta indicator

            windowData[8] = 0x05;       // Add and Run Data length
            windowData[9] = 0x0F;       // Instructions section length
            windowData[10] = 0x01;      // Addresses section length

            windowData[11] = 0x50;      // Add instruction data
            windowData[12] = 0x51;
            windowData[13] = 0x52;
            windowData[14] = 0x53;

            windowData[15] = 0x54;      // Run instruction data

            // Instructions section
            windowData[16] = 0x01;      // ADD  | Size 4  | No mode
            windowData[17] = 0x04;
            windowData[18] = 0x00;

            windowData[19] = 0x02;      // RUN  | Size 2  | No mode
            windowData[20] = 0x02;
            windowData[21] = 0x00;

            windowData[22] = 0x03;      // COPY | Size 8  | No mode
            windowData[23] = 0x08;
            windowData[24] = 0x00;

            windowData[25] = 0x00;      // NOOP | No Size | No mode
            windowData[26] = 0x00;
            windowData[27] = 0x00;

            windowData[28] = 0x00;      // NOOP | No Size | No mode
            windowData[29] = 0x00;
            windowData[30] = 0x00;

            windowData[31] = 0x10;      // Addresses section

            RebuildWindows rebuildWindows = new RebuildWindows();
            return rebuildWindows.RebuildWindow(windowData);
        }
    }
}
